
window.onload=function(){
	init_addImage();
	changeType();
	changeAdType();
}
$(".div_show").mouseover(function(){
	if( $(this).children("img").attr("src")&&$(this).children("img").attr("data-type")!="1"){
		$(this).find(".delete").show();
	}
  });
  $(".div_show").mouseout(function(){
	$(this).find(".delete").hide();
  });
$(".delete").click(function(){
		var fileinput=document.getElementById($(this).attr("data-id"));
		var img=$(this).parent().prev("img");
		var type=$(img).attr("data-type");
		layer.confirm("确定清空已选中的图片?",function(){
			layer.closeAll('dialog');
			if(type==2){
				img.removeAttr("src");
				img.removeAttr("data-type");
			}else if(type==3){
				img.attr("src",img.attr("data-oldsrc"));
				img.attr("data-type","1");
			}
			fileinput.outerHTML=fileinput.outerHTML;  
			init_addImage();
		});
});
function init_addImage(){
	/* 缩略图 */
	$(".addImage").change(function() {
		if (typeof (FileReader) != "undefined") {
			var dvPreview = $("#" + $(this).attr("data-id"));
			var regex = /(.jpg|.jpeg|.gif|.png|.bmp)$/;
			$($(this)[0].files).each(function() {
				var file = $(this);
				if (regex.test(file[0].name.toLowerCase())) {
					var reader = new FileReader();
					reader.onload = function(e) {
						var img =dvPreview.children("img");
						if(img.attr("src")&&img.attr("data-type")=='1'){
							img.attr("data-oldsrc", img.attr("src"));
							img.attr("data-type","3");
						}else{
							img.attr("data-type","2");
						}
						img.attr("src", e.target.result);			
						img.css({
							height : dvPreview.css("height"),
							width : dvPreview.css("width")
						});
					}
					reader.readAsDataURL(file[0]);
				} else {
					layer.alert(file[0].name + "不是图片文件!");
					var fileinput = document.getElementById(dvPreview.find('.delete').attr('data-id'));
					fileinput.outerHTML = fileinput.outerHTML;
					init_addImage();
					return false;
				}
			});
		} else {
			layer.alert("抱歉!浏览器版本太低了!不支持预览图片,换个浏览器试试吧~");
		}
	});
}


  
 //表单提交验证
	 function checkForm()
	 {
		 var title=$('#title').val();
	     var upload=$('#upload').val();
	     var position=$('#position').val();
	     var type =$('#type').val() ;
	     var url=$('#url').val();
	     var promotionID=$('#promotionId').val();
	     var images=$('#images');
	     var adDetail = CKEDITOR.instances.adDetailContent.getData();
	    $('#adDetailContent').innerHTML = adDetail;
	     
	 var adType = "";
	 var adTypes = document.getElementsByName("adType");
	 for(var i=0;i<adTypes.length;i++){
		 if(adTypes[i].checked){
			 adType = adTypes[i].value;
		 }
	 }
	 
	    if(type=="0") {
	     layer.alert("请选择版块名");
	     return ;
     }
	    
	    
	 if(trim(title).length==0)
     {
	     layer.alert("请输入广告主题");
	     return ;
     }
	    if(title.length > 200)
     {
	     layer.alert("广告主题长度不能超过200");
	     return ;
     }
     
	    //跑马灯文字广告
     if(type != 4){
    	 if(upload.length==0 && images.attr('data-type')!='1')
	     {
		     layer.alert("请上传图片");
		     return ;
	     }else if(upload.length>0){
    	     var isImag = picIsRule(upload,true);
    	     if(!isImag){
    			 return false;
    		  }	
    	 }
		
     }
    
     if(type != 5 && type !=7 && (position=="0" || position == null))
        {
	     layer.alert("请选择顺序!");
	     return;
     }
     
     if(type == 4 && adType == 2){
//	    	 if(trim(adDetail).length == 0){
//	    		 layer.alert("请输入广告详细");
//	    		 document.Promotion.content.focus();
//	    		 return;
//	    	 }
     }else if(type != 5 && type !=7){
    	 if(trim(url).length==0)
	     {
		     layer.alert("请输入广告链接!");
		     return ;
	     }
	     if(url.length > 200)
	     {
		     layer.alert("广告链接长度不能大于200");
		     return ;
	     }
     }
     var layer_load_window = layer.load(2); 
     $.ajax({
    	 url:path+"/promotion/check_create_promotion",
    	 data:{type:type,position:position,promotionID:promotionID},
    	 type:"post",
    	 success:function(data){
    		 if(data != "")  
		       {
		          layer.alert(data);
		          document.getElementById("position").value="0";
		       }
		       else
		       {  
			        var img = new Image(); 
					img.src = document.getElementById("images").src;
					if(type==1&&(img.width!=770||img.height!=260)){
						layer.confirm("与目标尺寸770*260大小不同可能会造成图片拉伸，确定上传？",function(){
				    		layer.closeAll('loading');
				            var layer_load_window = layer.load(2); 
							$("#save_form").submit(); 
						});
					}else{
			    		layer.closeAll('loading');
			            var layer_load_window = layer.load(2); 
						$("#save_form").submit(); 
					}
		       }
    	 },complete: function(XMLHttpRequest, textStatus) {
    			layer.close(layer_load_window);
    	 }
     });
}
 
 function changeType(){
	 var type =$('#type').val();
	 if(type == 4){
		 $("#advertImage").hide();
		 $("#adType").show();
		 $("#highlight").show();
		// document.getElementsByName("adType")[0].checked = true;
		 $("#adLink").show();
		 $("#positionTr").show();
		 $("#advertImage").val("");
		 $("#imageDescription").hide();
	 }else if(type == 5 || type == 7){
		 $("#advertImage").show();
		 if(type == 5){
			 $("#description").html("建议上传1920*400尺寸，jpg、gif、png或者bmp格式，单张图片大小不超过2MB");
		 }else if(type == 7){
			 $("#description").html("建议上传1080*354尺寸，jpg、gif、png或者bmp格式，单张图片大小不超过2MB");
		 }
		 $("#imageDescription").show();
		 $("#adType").hide();
		 $("#positionTr").hide();
		 $("#highlight").hide();
		 $("#adLink").hide();
		 $("#adDetail").hide();
	 }else{
		 $("#advertImage").show();
		 $("#adLink").show();
		 $("#adType").hide();
		 $("#highlight").hide();
		 $("#positionTr").show();
		 $("#adDetail").hide();
		 $("#imageDescription").hide();
	 }
 }
 
 function changeAdType(){
	 var adType = "";
	 var adTypes = document.getElementsByName("adType");
	 for(var i=0;i<adTypes.length;i++){
		 if(adTypes[i].checked){
			 adType = adTypes[i].value;
		 }
	 }
	 if(adType == "1"){
		 $("#adLink").show();
		 $("#adDetail").hide();
	 }if(adType == "2"){
		 $("#adLink").hide();
		 $("#adDetail").show();
	 }
 }
 function callback(success,message){
		layer.closeAll('loading');
		if(success == true){
			layer.msg('操作成功!',{icon:1,time:1000},function(){
				var index = parent.layer.getFrameIndex(window.name);
				parent.$('#page').val('1');
				parent.$('#search_form').submit();
				parent.layer.close(index);
			});
		}else{
			layer.alert(message);
		}
	}
 function trim(str){ 
 	return  str.replace(/^\s+|\s+$/g,"");
 }
 function cancle() {
 	var index = parent.layer.getFrameIndex(window.name);
 	parent.layer.close(index);
 }	